package com.sparrow.budejieproject.helper;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by wenquan on 16/6/13.
 */
public abstract class RecyclerViewAdapterHelper<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<T> list = null;
    private LayoutInflater mInflater = null;
    private OnItemClickedListener onItemClickedListener;
    private RecyclerView recyclerView=null;

    public interface OnItemClickedListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);
    }

    public void setOnItemClickedListener(
            OnItemClickedListener onItemClickedListener) {
        this.onItemClickedListener = onItemClickedListener;
    }

    public RecyclerViewAdapterHelper(Context context,List<T> list,RecyclerView recyclerView){
        this.list = list;
        this.recyclerView = recyclerView;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public abstract RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType);



    @Override
    public abstract void onBindViewHolder(RecyclerView.ViewHolder holder, int position);

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void reloadListView(List<T> _list,boolean isClear){
        if (isClear){
            list.clear();
            list.addAll(_list);
        }else{
            list.addAll(_list);
        }
        notifyDataSetChanged();
    }

    public abstract class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }


        @Override
        public void onClick(View v) {
            if (null != onItemClickedListener) {
                int position = recyclerView.getChildPosition(v);
                onItemClickedListener.onItemClick(position);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onItemClickedListener) {
                int position = recyclerView.getChildPosition(v);
                return onItemClickedListener.onItemLongClick(position);
            }
            return false;
        }
    }


}
